package com.aistar.util.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

public class AliOssUtil {

    // endpoint是访问OSS的域名。如果您已经在OSS的控制台上 创建了Bucket，请在控制台上查看域名。
    // endpoint的格式形如“http://oss-cn-hangzhou.aliyuncs.com/”，注意http://后不带bucket名称，
    private static final String ENDPOINT = "";

    // accessKeyId和accessKeySecret是OSS的访问密钥，您可以在控制台上创建和查看，
    // 创建和查看访问密钥的链接地址是：https://ak-console.aliyun.com/#/。
    // 注意：accessKeyId和accessKeySecret前后都没有空格，从控制台复制时请检查并去除多余的空格。
    private static final String ACCESS_KEY_ID = "";
    private static final String ACCESS_KEY_SECRET = "";

    // Bucket用来管理所存储Object的存储空间，详细描述请参看“开发人员指南 > 基本概念 > OSS基本概念介绍”。
    // Bucket命名规范如下：只能包括小写字母，数字和短横线（-），必须以小写字母或者数字开头，长度必须在3-63字节之间。
    private static final String BUCKET_NAME = "";


    /**
     * 以字符数组的形式上传文件
     * @param fileName
     * @param bytes
     * @return
     */
    public static String upload(String fileName,byte[] bytes){
        // 生成OSSClient
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID,ACCESS_KEY_SECRET);
        //创建上传对象
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, fileName, new ByteArrayInputStream(bytes));
        //获取http头
        ObjectMetadata metaData = ContentTypeUtil.getMetaData(fileName);
        putObjectRequest.setMetadata(metaData);
        //开始上传
        ossClient.putObject(putObjectRequest);
        //获取访问的url，设置url的失效时间为6个月
        Date expiration =  new Date(System.currentTimeMillis() + 6*30*24*60*60*1000l);
        //获取url
        URL url = ossClient.generatePresignedUrl(BUCKET_NAME, fileName, expiration);

        //关闭上传
        ossClient.shutdown();

        return getUrl(url);
    }

    /**
     * 以IO文件的形式上传
     * @param fileName
     * @param file
     * @return
     */
    public static String upload(String fileName, File file){
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID,ACCESS_KEY_SECRET);
        //创建上传对象
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, fileName, file);
        //获取http头
        ObjectMetadata metaData = ContentTypeUtil.getMetaData(fileName);
        putObjectRequest.setMetadata(metaData);
        //上传文件
        ossClient.putObject(putObjectRequest);
        //获取访问的url，设置url的失效时间为6个月
        Date expiration =  new Date(System.currentTimeMillis() + 6*30*24*60*60*1000l);
        //获取url
        URL url = ossClient.generatePresignedUrl(BUCKET_NAME, fileName, expiration);

        return getUrl(url);
    }

    public static String upload(String fileName, InputStream is){
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID,ACCESS_KEY_SECRET);
        //创建上传对象
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, fileName, is);
        //获取http头
        ObjectMetadata metaData = ContentTypeUtil.getMetaData(fileName);
        putObjectRequest.setMetadata(metaData);
        //上传文件
        ossClient.putObject(putObjectRequest);
        //获取访问的url，设置url的失效时间为6个月
        Date expiration =  new Date(System.currentTimeMillis() + 6*30*24*60*60*1000l);
        //获取url
        URL url = ossClient.generatePresignedUrl(BUCKET_NAME, fileName, expiration);

        return getUrl(url);
    }


    /**
     * 处理获得图片的路径
     * @param url
     * @return
     */
    public static String getUrl(URL url){
        String[] urlList = url.toString().split("[?]");
        String urlStr = urlList[0].replace("http","https");
        System.out.println(urlStr);
        return urlStr;
    }



}
